<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/25
 * Time: 21:31
 */

namespace app\admin\model;


use app\common\model\BaseModel;

class Logistical extends BaseModel
{
    const FREIGHT_TYPE_0 = 0;//0 所有商品统一运费 1 按件数计算运费 2  按重量（kg计算运费）
    const FREIGHT_TYPE_1 = 1;
    const FREIGHT_TYPE_2 = 2;

    const is_dis_0 = 0;  //是否支持物流   0 不支持 1支持
    const is_dis_1 = 1;

    /**
     * @return \think\model\relation\HasOne
     * 一对一获取对应物流规则
     */
    public function logsticalRrule(){
        return $this->hasOne('LogisticalRrule',"logistical_id","id");
    }


    /**
     * @param $freightType
     * @return bool
     * 物流费用类型判断
     */
    public function verificationFreightType($freightType){
        $param[] = self::FREIGHT_TYPE_0;
        $param[] = self::FREIGHT_TYPE_1;
        $param[] = self::FREIGHT_TYPE_2;
        return in_array($freightType,$param);
    }

    /**
     * @param $logisticalId
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 查询当前商户物流费用
     */
    private function findLogistical($logisticalId){
        return self::where('id',"=",$logisticalId)->find();
    }

    /**
     * @param $data
     * @return Logistical
     * 修改物流费用
     */
    public function updateLogistical($data){
        $data['updateTime'] = date("Y-m-d h:i:s",time());
        $tenantDistributionId = $data['tenantDistributionId'];
        unset($data['tenantDistributionId']);
        return self::where("tenantDistributionId","=",$tenantDistributionId)->update($data);
    }


    /**
     * @param $data
     * @return int|string
     * 创建物流费用
     */
    private function createLogistical($data){
        return self::insert($data);
    }

    /**
     * @param $data
     * @return Logistical|int|string
     * 如果当前商户存在物流费用配置，则修改
     * 不存在则创建
     */
    public function saveLogistical($data){
        $logisticalId = $data['id'];



        $findlogistical = self::findLogistical($logisticalId);
        if($findlogistical){
            unset($data['id']);
            $res = self::updateLogistical($data);
            if($res){
                //修改商家配送状态updateLogistics
               $logisticalModel = new \app\common\model\Logistics();
               $param['status'] = $logisticalModel::STATUS_INVALID;
              return $logisticalModel->updateLogistics($param,$data['tenantDistributionId']);
            }else{
                return false;
            }
        }else{
          return self::createLogistical($data);
        }

    }

    /**
     * @param $type_id
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 根据typeId 获取物流对应信息
     */
    public function findTypeLogistical($type_id){
        return self::where("tenantDistributionId","=",$type_id)->find();
    }


}